package com.crm.service.salChance;

import com.crm.pojo.SalChance;
import com.crm.repository.SalChanceRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SalChanceServiceImpl implements SalChanceService {
    @Resource
    private SalChanceRepository salChanceRepository;

    @Override
    public Page<SalChance> findSalChancePageByMap(Map param, Pageable pageable) throws Exception {
        return salChanceRepository.findAll(new Specification<SalChance>() {

            @Override
            public Predicate toPredicate(Root<SalChance> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>();

                if (param.get("chcCustName") != null) {
                    predicates.add(cb.like(root.get("chcCustName"), "%" + param.get("chcCustName") + "%"));
                }

                if (param.get("chcTitle") != null) {
                    predicates.add(cb.like(root.get("chcTitle"), "%" + param.get("chcTitle") + "%"));
                }

                predicates.add(cb.equal(root.get("chcStatus"), 1));

                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        }, pageable);
    }

    @Override
    public Page<SalChance> findSalPlanPageByMap(Map param, Pageable pageable) throws Exception {
        return salChanceRepository.findAll(new Specification<SalChance>() {

            @Override
            public Predicate toPredicate(Root<SalChance> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>();

                if (param.get("chcCustName") != null) {
                    predicates.add(cb.like(root.get("chcCustName"), "%" + param.get("chcCustName") + "%"));
                }

                if (param.get("chcTitle") != null) {
                    predicates.add(cb.like(root.get("chcTitle"), "%" + param.get("chcTitle") + "%"));
                }

                if (param.get("chcLinkman") != null) {
                    predicates.add(cb.like(root.get("chcLinkman"), "%" + param.get("chcLinkman") + "%"));
                }

                predicates.add(cb.notEqual(root.get("chcStatus"), 1));

                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        }, pageable);
    }

    @Override
    public SalChance findSalChanceById(Long chcId) {
        return salChanceRepository.findById(chcId).get();
    }

    @Override
    public SalChance editSalChance(SalChance salChance) throws Exception {
        return salChanceRepository.save(salChance);
    }

    @Override
    public int delSalChance(Long chcId) throws Exception {
        return salChanceRepository.deleteSalChanceByChcId(chcId);
    }

    @Override
    public SalChance addSalChance(SalChance salChance) throws Exception {
        return salChanceRepository.save(salChance);
    }

    @Override
    public int editSlaChanceByChcIdAndChcStatus(String chcStatus, Long chcId) throws Exception {
        return salChanceRepository.editSalChanceByChcIdAndChcStatus(chcStatus, chcId);
    }
}
